﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Security.Principal;
using WordpressRestApi;

namespace WordpressDemo.Models
{    /// <summary>Model for the identity.</summary>
     /// <remarks>Manages encode / decode.</remarks>
    public class IdentityModel : GenericIdentity
    {
        #region Inner Types
        private class InnerRepresentation
        {
            public string Name { get; set; }
            public TokenInfo Token { get; set; }
        }
        #endregion

        /// <summary>Constructor from a token package.</summary>
        /// <param name="token">Token</param>
        /// <param name="displayName"></param>
        public IdentityModel(TokenInfo token, string displayName)
            : base(displayName)
        {
            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }
            if (token.AccessToken == null)
            {
                throw new ArgumentNullException(nameof(token.AccessToken));
            }
            if (string.IsNullOrWhiteSpace(token.BlogID))
            {
                throw new ArgumentNullException(nameof(token.BlogID));
            }
            if (token.BlogUrl == null)
            {
                throw new ArgumentNullException(nameof(token.BlogUrl));
            }
            if (displayName == null)
            {
                throw new ArgumentNullException(nameof(displayName));
            }

            AccessToken = token.AccessToken;
            BlogID = token.BlogID;
            BlogUrl = token.BlogUrl;
        }

        /// <summary>Display name of the user.</summary>
        public string DisplayName { get { return Name; } }

        /// <summary>Access token.</summary>
        public string AccessToken { get; private set; }

        /// <summary>ID of the blog related to the access token.</summary>
        public string BlogID { get; private set; }

        /// <summary>URL of the blog related to the access token.</summary>
        public string BlogUrl { get; private set; }

        /// <summary>Creates a client.</summary>
        /// <returns>Client.</returns>
        public WordpressClient GetWordpressClient()
        {
            return new WordpressClient(new TokenInfo
            {
                AccessToken = AccessToken,
                BlogID = BlogID,
                BlogUrl = BlogUrl
            });
        }

        /// <summary>Converts the model into a JSON representation.</summary>
        /// <returns></returns>
        public string ToJson()
        {
            var representation = new InnerRepresentation
            {
                Name = DisplayName,
                Token = new TokenInfo { AccessToken = AccessToken, BlogID = BlogID, BlogUrl = BlogUrl }
            };
            var json = JsonConvert.SerializeObject(
                representation,
                new JsonSerializerSettings { Formatting = Formatting.None });

            return json;
        }

        /// <summary>Converts a json representation to an object representation.</summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static IdentityModel ToModel(string json)
        {
            System.Diagnostics.Trace.TraceInformation(json);

            var representation = JsonConvert.DeserializeObject<InnerRepresentation>(json);

            return new IdentityModel(representation.Token, representation.Name);
        }
    }
}